
import UIKit

class ViewController: UIViewController {
    var number:Int!
    var correctAnswer:Int!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet var buttons: [UIButton]!
    @IBOutlet weak var answerBox: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if(buttons != nil){
            for button in buttons {
                button.tag = Int(arc4random_uniform(100))
                button.setTitle("\(button.tag)", forState: UIControlState.Normal)
            }
        }
        if(questionLabel != nil){
            let newNumber = Int(arc4random_uniform(100))
            correctAnswer = newNumber+number
            questionLabel.text = " what is \(number) + \(newNumber)"
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }

    @IBAction func calculateAndBack(sender: AnyObject) {
        let filledInNumber = answerBox.text
        if(filledInNumber == "" || Int(filledInNumber!) != correctAnswer){
            let alertController = UIAlertController(title: "Hey There", message: "Wrong number?", preferredStyle: .Alert)
            
            let defaultAction = UIAlertAction(title: "Re-calculate", style: .Default, handler: nil)
            alertController.addAction(defaultAction)
            
            presentViewController(alertController, animated: true, completion: nil)
        }else{
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        view.endEditing(true)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! ViewController
        let button = sender as! UIButton
        controller.number = button.tag
    }
}

